﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Bootstrap_Project.Web.Ports;
using Bootstrap_Project.Web.FixtureMaintenance;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Configuration;
using System.Text;
using Newtonsoft.Json;

namespace Bootstrap_Project.Web.Api_Services
{
    //--disable-web-security fix issue post request 404 error (only chrome)
    public class FixtureController : ApiController
    {

        #region random generation
        public static List<MaintenanceFixtureTC> listOfFixes = new List<MaintenanceFixtureTC>();
        static Random random = new Random((int)DateTime.Now.Ticks);
        public static bool check = true;
        public static void FillFixtures(int amount)
        {
            if (check)
                for (int i = 0; i < amount; i++)
                {
                    MaintenanceFixtureTC obj = new MaintenanceFixtureTC();
                    obj.AdditionalWorkspaceUrl = GenerateRndStr();
                    obj.BillOfLadenDate = GenerateRndDt();
                    obj.Charterer = GenerateRndAcc();
                    obj.ChartererOperator = GenerateRndAcc();
                    obj.CoBrokers = GenerateRndCbr();
                    obj.CommissionInvoiceDate = GenerateRndDt();
                    obj.ContractDate = GenerateRndDt();
                    obj.CPDate = GenerateRndDt();
                    obj.DemurrageAgreedDate = GenerateRndDt();
                    obj.DemurrageAmount = random.Next(1, 1000);
                    obj.DemurrageStatus = EDemurrageStatus.Accepted;
                    obj.DischargeOrderDate = GenerateRndDt();
                    obj.DischargePorts = GenerateRndPorts();
                    obj.ErrorMessage = GenerateRndStr();
                    //obj.ExtensionData ???
                    obj.FixtureNumber = GenerateRndStr();
                    obj.FreightInvoiceDate = GenerateRndDt();
                    obj.Id = i.ToString();
                    obj.IsValid = GenerateRndBool();
                    obj.LayCan = GenerateRndLay();
                    obj.LetterOfIndemnityDate = GenerateRndDt();
                    obj.LinkToDemurrage = GenerateRndStr();
                    obj.LoadingOrderDate = GenerateRndDt();
                    obj.LoadingPorts = GenerateRndPorts();
                    obj.Name = GenerateRndStr();
                    obj.Owner = GenerateRndAcc();
                    obj.OwnerOperator = GenerateRndAcc();
                    obj.SelectedDischargePort = 0;
                    obj.SelectedLoadingPort = 0;
                    obj.Status = GenerateRndStr();
                    obj.Type = GenerateRndStr();
                    obj.Vessel = GenerateRndVessel();
                    obj.VesselFleet = GenerateRndStr();
                    obj.VesselIMO = random.Next(2, 6);
                    obj.VesselName = GenerateRndStr();
                    //obj.VoyageOrderDate = GenerateRndDt();
                    obj.VoyageRef = GenerateRndStr();
                    obj.WorkspaceUrl = GenerateRndStr();
                    listOfFixes.Add(obj);
                    check = false;
                }
        }

        private static Bootstrap_Project.Web.FixtureMaintenance.Vessel GenerateRndVessel()
        {
            Bootstrap_Project.Web.FixtureMaintenance.Vessel obj = new Bootstrap_Project.Web.FixtureMaintenance.Vessel();
            //obj.ExtensionData ???
            obj.Name = GenerateRndStr();
            return obj;
        }

        private static LayCan GenerateRndLay()
        {
            LayCan obj = new LayCan();
            //obj.ExtensionData ???
            obj.FromDate = GenerateRndDt();
            obj.ToDate = GenerateRndDt();
            return obj;
        }

        private static Bootstrap_Project.Web.FixtureMaintenance.Port[] GenerateRndPorts()
        {
            Bootstrap_Project.Web.FixtureMaintenance.Port[] arrayOfPorts = new Bootstrap_Project.Web.FixtureMaintenance.Port[random.Next(3, 6)];
            for (int i = 0; i < arrayOfPorts.Length; i++)
            {
                arrayOfPorts[i] = new Bootstrap_Project.Web.FixtureMaintenance.Port();
                //arrayOfPorts[i].AAMarker = "Anchor aweigh";
                arrayOfPorts[i].AATime = GenerateRndDt();
                arrayOfPorts[i].Chat = GenerateRndChat();
                arrayOfPorts[i].ETA_NORTime = GenerateRndDt();
                //arrayOfPorts[i].ETAMarker = "NOR";
                arrayOfPorts[i].ETB_ATBTime = GenerateRndDt();
                //arrayOfPorts[i].ETBMarker = "ETB";
                //arrayOfPorts[i].ETCMarker = GenerateRndStr();
                arrayOfPorts[i].ETD_ATDTime = GenerateRndDt();
                //arrayOfPorts[i].ETDMarker = "HD";
                //arrayOfPorts[i].ExtensionData ???
                //arrayOfPorts[i].FinalCalculationOption = 0;
                arrayOfPorts[i].Laytime = random.Next(2, 20);
                //arrayOfPorts[i].Name = GenerateRndStr();
                arrayOfPorts[i].Notes = GenerateRndStr();
                //arrayOfPorts[i].Sequence = i;
                arrayOfPorts[i].SldDate = GenerateRndDt();
                arrayOfPorts[i].Zone = GenerateRndStr();
            }
            return arrayOfPorts;
        }

        private static ChatItem[] GenerateRndChat()
        {
            ChatItem[] arrayofObjs = new ChatItem[random.Next(2, 6)];
            for (int i = 0; i < arrayofObjs.Length; i++)
            {
                arrayofObjs[i] = new ChatItem();
                //obj.ExtensionData ???
                arrayofObjs[i].Message = GenerateRndStr();
                arrayofObjs[i].SenderName = GenerateRndStr();
                arrayofObjs[i].Time = GenerateRndDt();
            }
            return arrayofObjs;
        }

        private static CoBroker[] GenerateRndCbr()
        {
            CoBroker[] arrayOfObjs = new CoBroker[random.Next(3, 6)];
            for (int i = 0; i < arrayOfObjs.Length; i++)
            {
                arrayOfObjs[i] = new CoBroker();
                arrayOfObjs[i].BrokeForChartrer = GenerateRndBool();
                arrayOfObjs[i].BrokeForOwner = GenerateRndBool();
                arrayOfObjs[i].CollectCommission = GenerateRndBool();
                arrayOfObjs[i].CommissionAmount = random.Next(1, 10000);
                arrayOfObjs[i].CommissionPercentage = random.Next(1, 100);
                arrayOfObjs[i].CrmUrl = GenerateRndStr();
                //obj.ExtensionData ???
                arrayOfObjs[i].Name = GenerateRndStr();
                arrayOfObjs[i].OperatorCrmUrl = GenerateRndStr();
                arrayOfObjs[i].OperatorName = GenerateRndStr();
            }
            return arrayOfObjs;
        }

        private static bool GenerateRndBool()
        {
            return Convert.ToBoolean(random.Next(2));
        }

        private static Account GenerateRndAcc()
        {
            Account obj = new Account();
            obj.Id = random.Next(0, 100).ToString();
            obj.MobilePhone = GenerateRndStr();
            obj.Name = GenerateRndStr();
            obj.OfficePhone = GenerateRndStr();
            obj.SelectedContactId = random.Next(0, 100).ToString();
            obj.GeneralEmail = GenerateRndStr();
            //obj.ExtensionData ????
            obj.DistributionListLink = GenerateRndStr();
            obj.DirectEmail = GenerateRndStr();
            obj.CrmUrl = GenerateRndStr();
            obj.Contacts = GenerateRndCntct();
            return obj;
        }

        private static Contact[] GenerateRndCntct()
        {
            Contact[] arrayOfObjs = new Contact[random.Next(3, 6)];
            for (int i = 0; i < arrayOfObjs.Length; i++)
            {
                arrayOfObjs[i] = new Contact();
                arrayOfObjs[i].AccountRef = GenerateRndStr();
                arrayOfObjs[i].EditUrl = GenerateRndStr();
                arrayOfObjs[i].EmailAddress = GenerateRndStr();
                //obj.ExtensionData ???
                arrayOfObjs[i].Id = i.ToString();
                arrayOfObjs[i].InstantMessagingAddress1 = GenerateRndStr();
                arrayOfObjs[i].InstantMessagingAddress2 = GenerateRndStr();
                arrayOfObjs[i].JobTitle = GenerateRndStr();
                arrayOfObjs[i].MobilePhone = GenerateRndStr();
                arrayOfObjs[i].Name = GenerateRndStr();
                arrayOfObjs[i].Telephone = GenerateRndStr();
            }
            return arrayOfObjs;
        }

        private static string GenerateRndStr()
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < random.Next(4, 10); i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            return builder.ToString();
        }

        private static DateTime GenerateRndDt()
        {
            return new DateTime(random.Next(1970, 2014), random.Next(1, 12), random.Next(1, 28));
        }
        #endregion

        //load random fixtures
        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpGet]
        public List<MaintenanceFixtureTC> Get(string OperatorId, string fixtureType)
        {
            FixtureController.FillFixtures(100);
            return FixtureController.listOfFixes;
        }

        //Save fixtures
        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpPost]
        public void Post(ObservableCollection<MaintenanceFixtureTC> fixtures)
        {
            string xmlString = FixtureExtendController.fixClient.PerformOperation("Save",
                string.Format("xmlSerializedObject:{0}", Serializer.Serialize<ObservableCollection<MaintenanceFixtureTC>>(fixtures)));
        }

        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpGet]
        public ObservableCollection<Bootstrap_Project.Web.Ports.Port> GetPorts(string name)
        {
            string xml = FixtureExtendController.portClient.PerformOperation("find", string.Format("name:{0}", name)); //n0t underst00d
            Bootstrap_Project.Web.Ports.Reciept r = Serializer.Deserialize<Bootstrap_Project.Web.Ports.Reciept>(xml);
            ObservableCollection<Bootstrap_Project.Web.Ports.Port> res = new ObservableCollection<Bootstrap_Project.Web.Ports.Port>();
            if (r.Errors.Length == 0)
            {
                res = Serializer.Deserialize<ObservableCollection<Bootstrap_Project.Web.Ports.Port>>(r.ResponsePayload);
                for (int i = 0; i < res.Count; i++)
                    if (!res[i].Name.ToLower().StartsWith(name.ToLower()))
                    {
                        res.RemoveAt(i);
                        i--;
                    }
            }
            return res;
        }
    }
}
